package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seawaterbt.ssm.entity.*;
import com.seawaterbt.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ZDatBoiler {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    CzDatBoilerdataService czDatBoilerdataService;
    @Autowired
    CzBasBoilerService basBoilerService;
    @Autowired ZDatBoilerDataService zDatBoilerDataService;
    @Autowired CzDatBoilerdataHistoryService czDatBoilerdataHistoryService;
    public void sendData() {
        QueryWrapper<CzDatBoilerdata> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("boilder_code", 130902008).or().likeRight("boilder_code", 130903007);
        Date now = new Date();
        List<CzDatBoilerdata> czDatBoilerdataList = czDatBoilerdataService.list(queryWrapper);
        for (CzDatBoilerdata czDatBoilerdata : czDatBoilerdataList) {
            CzBasBoiler one = basBoilerService.listOne(czDatBoilerdata.getBoilderCode());
            //&& one.getCodeProvince() != null && !one.getCodeProvince().equals("")
            if (one != null ) {
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("BoilerID", one.getCode());
                wrapper.eq("GetTime", czDatBoilerdata.getGetTime());
                ShengDatBoilerData byCodeTime = zDatBoilerDataService.getByCodeTime(wrapper);
                if (byCodeTime == null) {
                    ShengDatBoilerData shengData = new ShengDatBoilerData();
                    shengData.setBoilerID(one.getOriginalCode());
                    shengData.setAddTime(now);
                    shengData.setGetTime(czDatBoilerdata.getGetTime());
                    shengData.setBackTemp(StringUtils.isEmpty(czDatBoilerdata.getBackTemp())?0:Double.parseDouble(czDatBoilerdata.getBackTemp()));
                    shengData.setSupplyTemp(StringUtils.isEmpty(czDatBoilerdata.getSupplyTemp())?0:Double.parseDouble(czDatBoilerdata.getSupplyTemp()));
                    shengData.setLoadRate(StringUtils.isEmpty(czDatBoilerdata.getLoadRate())?0:Double.parseDouble(czDatBoilerdata.getLoadRate()));
                    shengData.setRatedLoad(StringUtils.isEmpty(czDatBoilerdata.getRatedLoad())?0:Double.parseDouble(czDatBoilerdata.getRatedLoad()));
                    zDatBoilerDataService.save(shengData);
                }
            }
        }

        System.out.println("完成同步锅炉到中间库 " + df.format(new Date()));
    }


    //拉取数据
    public void getData() {
        QueryWrapper<ShengDatBoilerData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("BoilerID", 130902008).or().likeRight("BoilerID", 130903007);
        List<ShengDatBoilerData> shengDatBoilerDataList = zDatBoilerDataService.list(queryWrapper);
        for (ShengDatBoilerData shengDatBoilerData:shengDatBoilerDataList){
            CzBasBoiler one = basBoilerService.GetOneByProviencecode(shengDatBoilerData.getBoilerID());
            if (one != null ) {
                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("boilder_code", one.getCode());
                wrapper.eq("get_time", shengDatBoilerData.getGetTime());
                CzDatBoilerdata byCodeTime = czDatBoilerdataService.getByCodeTime(wrapper);
                if (byCodeTime == null) {
                    CzDatBoilerdata czDatBoilerdata = new CzDatBoilerdata();
                    czDatBoilerdata.setBackTemp(shengDatBoilerData.getBackTemp()+"");
                    czDatBoilerdata.setBoilderCode(byCodeTime.getBoilderCode());
                    czDatBoilerdata.setGetTime(shengDatBoilerData.getGetTime());
                    czDatBoilerdata.setLoadRate(shengDatBoilerData.getLoadRate()+"");
                    czDatBoilerdata.setRatedLoad(shengDatBoilerData.getRatedLoad()+"");
                    czDatBoilerdata.setSupplyTemp(shengDatBoilerData.getSupplyTemp()+"");
                    czDatBoilerdataService.saveOrUpdate(czDatBoilerdata);

                    CzDatBoilerdataHistory czDatBoilerdataHistory = new CzDatBoilerdataHistory();
                    czDatBoilerdataHistory.setBackTemp(shengDatBoilerData.getBackTemp()+"");
                    czDatBoilerdataHistory.setBoilderCode(byCodeTime.getBoilderCode());
                    czDatBoilerdataHistory.setGetTime(shengDatBoilerData.getGetTime());
                    czDatBoilerdataHistory.setLoadRate(shengDatBoilerData.getLoadRate()+"");
                    czDatBoilerdataHistory.setRatedLoad(shengDatBoilerData.getRatedLoad()+"");
                    czDatBoilerdataHistory.setSupplyTemp(shengDatBoilerData.getSupplyTemp()+"");
                    czDatBoilerdataHistoryService.save(czDatBoilerdataHistory);
                }
            }
        }
    }

}
